// pages/detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:"",
    time:"",
    count:"",
    content:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNewDetail(options["id"])
  },
  getNewDetail(newId){
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://test-miniprogram.com/api/news/detail',
      data:{
        "id":newId
      },
      success: function(res){
        console.log(res)
        if (res.data.code != 200) {
          wx.showToast({
            title: res.data.message,
            icon:"none"
          })
          return
        }
        //set data
        that.setNewsDetail(res.data.result)
      },
      complete: function(){
        wx.hideLoading()
      }
    })
  },
  setNewsDetail(data){
    let time = this.formateSourceAndCity(data.source,data.date)
    let count = "阅读 " + data.readCount
    this.setData({
      title: data.title,
      time: time,
      count: count,
      content: data.content
    })
  }
  ,
  backTap: function(){
    wx.navigateBack({
      
    })
  },
  formateSourceAndCity(source, time) {
    if (source == null || source.length == 0) {
      source = "默认新闻"
    }
    let date = new Date(time)
    return source + "      " + date.getHours() + ":" + date.getMinutes()
  }
})